package com.example.Lezedata.ui.activity.login;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.common.TransitionPage;
import com.example.Lezedata.ui.customView.CGInputAndRightView;
import com.example.Lezedata.ui.customView.CGInputView;
import com.example.Lezedata.ui.fragment.commonfragment.inputTextField;
import com.example.Lezedata.util.GBInterface;
import com.example.Lezedata.util.GBSaveData;
import com.example.Lezedata.util.GBToast;
import com.example.Lezedata.util.GBUtil;

import bid.Bid;

public class ForgetPsdVc extends GBBaseVc {
    private TextView rightView;
    private CGInputView textField1;
    private CGInputAndRightView textField2;
    boolean isPhoneLogin = true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forgetpsd;
    }

    @Override
    public CharSequence title() {
        return getResources().getString(R.string.forgetPsdTitle);
    }

    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        findViewById(R.id.commit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneCode = textField1.leftView.getText().toString();
                String phone = textField1.inputView.getText().toString();
                String code = textField2.inputView.getText().toString();
                if (GBUtil.stringIsEmpty(phone)){
                    GBToast.shortToast("手机号码不能为空");
                    return;
                }
                if (GBUtil.stringIsEmpty(code)){
                    GBToast.shortToast("验证码不能为空");
                    return;
                }
                Intent intent = new Intent(mContext,SetPsdVc.class);
                intent.putExtra("phoneCode",phoneCode);
                intent.putExtra("phone",phone);
                intent.putExtra("code",code);
                intent.putExtra("isPhoneLogin",isPhoneLogin);
                jumpActivityIntent(intent);
            }
        });
        findViewById(R.id.switchAccountType).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isPhoneLogin = !isPhoneLogin;
                switchLoginType();
            }
        });
        initView();
        switchLoginType();
    }

    void switchLoginType(){
        String login_phone_code = "";
        String login_phone = "";

        if (login_phone_code == ""){
            login_phone_code = "+86";
        }
        String hint = getString(R.string.EnterMobile);
        int inputType = InputType.TYPE_CLASS_NUMBER;
        int limit = 11;

        if (!isPhoneLogin){ // 邮箱
            limit = 120;
            inputType = InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
            hint = "请输入邮箱地址";
            login_phone_code = "邮箱";
        }

        Drawable drawable = getResources().getDrawable(isPhoneLogin?R.drawable.email_type:R.drawable.phone_type);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                drawable.getMinimumHeight());
        TextView textView = findViewById(R.id.switchAccountType);
        textView.setCompoundDrawables(null,drawable,null,null);
        textView.setText(isPhoneLogin?"邮箱找回":"手机找回");

        textField1.inputView.setHint(hint);
        textField1.leftView.setText(login_phone_code);
        textField1.inputView.setText(login_phone);
        textField1.inputView.setInputType(inputType);
        InputFilter[]inputFilter = new InputFilter[]{GBUtil.limitFilter(limit)};
        textField1.inputView.setFilters(inputFilter);
    }

    void initView (){
        textField1 = findViewById(R.id.mobile);
        textField1.leftView.setText("+86");
        textField1.inputView.setHint(getString(R.string.EnterMobile));
        textField1.inputView.setInputType(InputType.TYPE_CLASS_NUMBER);
        InputFilter[] inputFilter = new InputFilter[]{GBUtil.limitFilter(11)};
        textField1.inputView.setFilters(inputFilter);

        textField2 = findViewById(R.id.password);
        textField2.leftView.setText(getString(R.string.Code));
        textField2.inputView.setHint(getString(R.string.EnterCode));
        rightView = textField2.rightView;
        refreshRightView();
        rightView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendCode(v);
            }
        });

        textField2.inputView.setInputType(InputType.TYPE_CLASS_NUMBER);
        InputFilter[] inputFilter1 = new InputFilter[]{GBUtil.limitFilter(6)};
        textField2.inputView.setFilters(inputFilter1);
    }

    void refreshRightView(){
        rightView.setGravity(Gravity.CENTER);
        rightView.setText("获取");
        rightView.setTextColor(getResources().getColor(R.color.app_color_main));
        rightView.setTextSize(16);
        Drawable drawable = getResources().getDrawable(R.drawable.corner_store_miancolor_19dp);
        rightView.setBackground(drawable);
        FrameLayout.LayoutParams viewParams = (FrameLayout.LayoutParams) rightView.getLayoutParams();
        viewParams.rightMargin = GBUtil.dpTo2px(mContext,0);
        viewParams.width = GBUtil.dpTo2px(mContext,77);
        viewParams.height = GBUtil.dpTo2px(mContext,38);
        rightView.setLayoutParams(viewParams);
    }

    void sendCode(View view){
        if (isPhoneLogin){
            sendPhoneCode();
        }else {
            sendEmailCode();
        }
    }

    void sendEmailCode(){
        String email = textField1.inputView.getText().toString();
        String code = textField1.leftView.getText().toString();
        if (GBUtil.stringIsEmpty(email)){
            GBToast.shortToast("邮箱地址不能为空");
            return;
        }
        Bid.GetSmsCodeRequest smsCodeRequest = Bid.GetSmsCodeRequest.newBuilder()
                .setPhoneCode(code)
                .setSmsType(Bid.SmsType.ST_RESET_PW)
                .setEmail(email)
                .build();
        GBNetRequest.stub().getSmsVerificationCode(smsCodeRequest,ServerResponseObserver.observer(new ServerResponseObserver.requestListener<Bid.SmsCodeResponse>() {
            @Override
            public void complete(Bid.SmsCodeResponse value, GBError error) {
                if (error == null){
                    GBToast.shortToast("邮件发送成功！");
                }
            }
        }));
    }

    void sendPhoneCode(){
        String phone = textField1.inputView.getText().toString();
        String phoneCode = textField1.leftView.getText().toString();
        if (GBUtil.stringIsEmpty(phone)){
            GBToast.shortToast("手机号码不能为空");
            return;
        }
        Bid.GetSmsCodeRequest smsCodeRequest = Bid.GetSmsCodeRequest.newBuilder()
                .setPhone(phone).setPhoneCode(phoneCode).setSmsType(Bid.SmsType.ST_RESET_PW).build();
        GBNetRequest.stub().getSmsVerificationCode(smsCodeRequest,ServerResponseObserver.observer(new ServerResponseObserver.requestListener<Bid.SmsCodeResponse>() {
            @Override
            public void complete(Bid.SmsCodeResponse value, GBError error) {
                if (error == null){
                    GBToast.shortToast("短信发送成功！");
                }
            }
        }));
    }
}